package cn.edu.ctbu.websql.controller;

import cn.edu.ctbu.websql.domain.Student;
import cn.edu.ctbu.websql.service.StudentServer;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/webapi/login")
public class LoginRestController {
    @Autowired
    StudentServer studentServer;

    @GetMapping("/user")
    public int user(HttpServletRequest httpServletRequest,String username, String password){
        //  判断用户密码是否存在
        List<Student> students = studentServer.findByNoAndPassword(username,password);

        if(!students.isEmpty()){
            HttpSession session=httpServletRequest.getSession();

            session.setAttribute("userId",students.get(0).getId());
            session.setAttribute("user",students.get(0).getNo());

            return 1;   //成功
        }
        return -1;  //失败
    }
}
